/**
 * Copyright 2004 Cordys R&D B.V. 
 * 
 * This file is part of the Cordys FTP Connector. 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 package com.eibus.applicationconnector.ftp;

import com.eibus.xml.nom.Node;
import com.eibus.xml.nom.Document;
import com.eibus.soap.fault.Fault;
import com.eibus.applicationconnector.ftp.localization.message.Messages;

import java.util.List;
import java.util.Vector;

/**
 * This class is the handler for FTP's LS command
 */
class GetListOfFiles extends FTPCommand
{
	private String directory;
	private boolean withDetails = false;
	private final List<String> listOfFiles = new Vector<String>();
	private final List<CordysFTPFile> listOfFilesWithDetails = new Vector<CordysFTPFile>();

	private String statusMsg;
    private Document document;

	@Override
	public boolean prepare()
	{
		int directoryNode = Node.getElementNS(requestXML, "directory", getNameSpace());
		directory = Node.getDataWithDefault(directoryNode, "");
		directory = getProperFileSeparator(directory, true);
		withDetails = Node.getAttribute(directoryNode, "details", "false").equals("true") ? true: false; 
		
		try
		{
			if (withDetails)
			{
				statusMsg = adapter.getListOfFilesWithDetails(directory, listOfFilesWithDetails);
			}
			else
			{
				statusMsg = adapter.getListOfFiles(directory, listOfFiles);
			}
		}
		catch (FTPException e)
		{
            Fault fault = response.createSOAPFault(Fault.Codes.CLIENT, Messages.SPECIFIED_FILE_NOT_FOUND);
            addFaultDetails2(fault.getDetail(),e,DEFAULT_QNAME,null,null);
            return false;
		}
		return true;
    }

    @Override
	public void commit()
    {
		int responseNode = response.getXMLNode();
        document = Node.getDocument(responseNode);

        addRequestDetailsToResponse(requestXML, responseNode);

        int directoryNode = Node.getElementNS(requestXML, "directory", getNameSpace());
        Node.createElementWithParentNS("directory", Node.getDataWithDefault(directoryNode, ""), responseNode);

		if (withDetails)
		{
			for(CordysFTPFile cordysFTPFile : listOfFilesWithDetails)
	        {
				int fileNode = document.createTextElement("file", cordysFTPFile.getFileName(), responseNode);
				if(!cordysFTPFile.isDirectory())
				{
					document.createTextElement("size", String.valueOf(cordysFTPFile.getFileSize()), fileNode);
				}
				document.createTextElement("lastmodified", cordysFTPFile.getFileModificationDate(), fileNode);
			}
		}
		else
		{
	        for(String filename : listOfFiles)
	        {
				document.createTextElement("file", filename, responseNode);
			}
		}
		
		if (ftpConfiguration.getProtocol().equals("SFTP"))
		{
			document.createTextElement("status", statusMsg, responseNode);			
		}
		else
		{
			document.createTextElement("statuscode", statusMsg.substring(0, 3), responseNode);
			document.createTextElement("status", statusMsg.substring(4), responseNode);			
		}
		
		/* After the command is complete, disconnect the adapter from the FTP server */
		adapter.disconnect();
    }
}